const {AppletBikeScore} = require('../db/seq')

class AppletBikeScoreServe {
    async addBikeScoreServe(params) {
        const {bikeId, score, userId} = params
        const res = await AppletBikeScore.findOne({
            where: {
                userId,
                bikeId
            }
        })
        if (!res) {
            await AppletBikeScore.create({
                userId,
                bikeId,
                score
            })
        } else {
            await AppletBikeScore.update({
                score
            }, {
                where: {
                    userId,
                    bikeId
                }
            })
        }
    }

    async getBikeScoreServe(ctx) {
        const {bikeId} = ctx.request.query
        const {id} = ctx.state.appletUser
        const res = await AppletBikeScore.findAll({
            where: {
                bikeId,
                userId: id
            }
        })
        return res ? res : null
    }
}

module.exports = new AppletBikeScoreServe()
